/* AST tree functions. */

#include "defs.h"
#include "data.h"
#include "decl.h"

/*
 * Build and return a generic AST node.
 */
struct ASTnode *mkastnode(int op, int type, struct ASTnode *left, 
                    struct ASTnode *mid, struct ASTnode *right, int intvalue)
{
    struct ASTnode *node;

    /* malloc a new ASTnode */
    node = (struct ASTnode *) malloc(sizeof(struct ASTnode));
    if (node == NULL)
        fatal("Unable to malloc in mkastnode()\n");

    /* copy in the field values and return it */
    node->op = op;
    node->type = type;
    node->left = left;
    node->mid = mid;
    node->right = right;
    node->v.intvalue = intvalue;

    return node;
} 

/*
 * Make an AST leaf node.
 */
struct ASTnode *mkastleaf(int op, int type, int intvalue)
{
    return mkastnode(op, type, NULL, NULL, NULL, intvalue);
}

/*
 * Make a unary node: only one child.
 */
struct ASTnode *mkastunary(int op, int type, 
                        struct ASTnode *left, int intvalue)
{
    return mkastnode(op, type, left, NULL, NULL, intvalue);
}
